import store, { type StoreBase } from "store2";

type StoreKey = keyof StoreBase;
type StoreType = 'local' | 'session';

const version = import.meta.env.ENV_APP_VERSION;
const storage = store.namespace(version);

const keys = [
  'clear:clear',
  'getItem:get',
  'setItem:set',
  'removeItem:remove'
];

export const getWebStorage = (type: StoreType = 'local') => {
  const s = storage[type];
  return keys.reduce((pre, val) => {
    const [key, key1] = val.split(':');
    return { ...pre, [key]: s[key1 as StoreKey].bind(s) };
  }, type === 'local' ? localStorage : sessionStorage);
}

export default storage;